const menuService = require("../services/menuService");

class MenuController {
  // 创建菜单
  async create (ctx) {
    const data = ctx.request.body;
    const user_id = ctx.state.user.user_id;
    await menuService.create(data, user_id);

    ctx.body = {
      code: 200,
      data: null,
      message: '创建成功'
    }
  }

  // 更新菜单
  async update (ctx) {
    const data = ctx.request.body;
    const user_id = ctx.state.user.user_id;
    const menu = await menuService.update(data, user_id);
    ctx.body = {
      code: 200,
      data: menu,
      message: '更新成功'
    }
  }

  // 删除菜单
  async delete (ctx) {
    const { id } = ctx.query;
    await menuService.delete(id);
    ctx.body = {
      code: 200,
      data: null,
      message: '删除成功'
    }
  }

  // 获取单个菜单
  async findOne (ctx) {
    const { id } = ctx.query;
    const menu = await menuService.findOne(id);
    ctx.body = {
      code: 200,
      data: menu,
      message: '查询成功'
    }
  }

  // 获取所有菜单
  async findAll (ctx) {
    const { type } = ctx.query;
    const menus = await menuService.findAll(type);
    ctx.body = {
      code: 200,
      data: menus,
      message: 'Success'
    }
  }
}

module.exports = new MenuController();